package com.tudulist.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.tudulist.dao.TodoListDao;
import com.tudulist.model.TodoList;
import com.tudulist.model.User;

@SuppressWarnings("serial")
@Repository
@Transactional
public class TodoListDaoImpl implements TodoListDao {

	@PersistenceContext
	EntityManager em;

	@SuppressWarnings("unchecked")
	public List<TodoList> findAll() {
		return em.createQuery("from TodoList").getResultList();
	}

	public TodoList find(long id) {
		return (TodoList) em.find(TodoList.class, id);
	}

	public TodoList save(TodoList todoList) {
		return em.merge(todoList);
	}

	public void remove(TodoList todoList) {
		em.remove(todoList);

	}

	public void remove(long id) {
		TodoList todolist = (TodoList) em.find(TodoList.class, id);
		if (null != todolist) {
			em.remove(todolist);
		}

	}

	@SuppressWarnings("unchecked")
	public List<TodoList> findAll(User user) {
		return em.createQuery("from TodoList where user_id=" + user.getId()).getResultList();
	}

}
